package com.yzh.grainmall.order.config;

import com.rabbitmq.client.Channel;
import com.yzh.grainmall.order.entity.OrderEntity;
import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @author yuzhen
 * @description创建RabbitMQ 队列 交换机
 * 运行之前，一定要小心，否则要删除队列/交换机重新运行 麻烦！
 *
 * 解决消息丢失(最怕)
 *  1 做好消息确认机制（publisher，consumer【手动ack】）
 *  2 每一个发送的消息都在数据库做好记录。定期将失败的消息再次发送一次
 * 解决消息重复
 *  1 幂等性
 *  2 防重表
 *  3 RabbitMQ自带redelivered (做法过于暴力)
 * 解决消息积压
 *  1 增加更多的消费者
 *  2 上线专门的队列消费服务，取出来，记录到数据库，离线慢慢处理
 */
@Configuration
public class MyMQConfig {

    // 容器中的组建Queue Exchange Binding 都会自动创建（前提是RabbitMQ没有）

    @Bean
    public Exchange orderEventExchange(){
        return new TopicExchange("order-event-exchange", true, false, null);
    }

    @Bean
    public Queue orderDelayQueue(){
        Map<String, Object> arguments = new HashMap<>();
        arguments.put("x-dead-letter-exchange", "order-event-exchange");//死信交换机
        arguments.put("x-dead-letter-routing-key", "order.release.order");//死信路由键
        arguments.put("x-message-ttl", 60000);//消息过期时间 ms 1分钟
        return new Queue("order-delay-queue", true, false, false, arguments);
    }

    @Bean
    public Queue orderReleaseOrderQueue() {
        //普通队列
        return new Queue("order.release.order.queue", true, false, false);
    }

    @Bean
    public Binding orderCreateOrderBinding(){
        //和延时队列绑定
        return new Binding("order-delay-queue", Binding.DestinationType.QUEUE,
                "order-event-exchange", "order.create.order", null);
    }

    @Bean
    public Binding orderReleaseOrderBinding() {
        //和普通队列绑定
        return new Binding("order.release.order.queue",
                Binding.DestinationType.QUEUE,
                "order-event-exchange",
                "order.release.order",
                null);
    }

    @Bean
    public Binding orderReleaseOtherBinding() {
        //订单释放直接和库存释放进行绑定
        return new Binding("stock.release.stock.queue",
                Binding.DestinationType.QUEUE,
                "order-event-exchange",
                "order.release.other.#",
                null);
    }

    @Bean
    public Queue orderSeckillOrderQueue() {
        //是否持久化的 是否排他的(大家都能监听，谁抢到算谁的) 是否自动删除
        return new Queue("order.seckill.order.queue", true, false, false);
    }

    @Bean
    public Binding orderSeckillOrderQueueBinding() {
        return new Binding("order.seckill.order.queue",
                Binding.DestinationType.QUEUE,
                "order-event-exchange",
                "order.seckill.order",
                null);
    }

    @RabbitListener(queues = "order.release.order.queue")
    public void listening(OrderEntity entity, Channel channel, Message message) throws IOException {
        System.out.println("收到过期的订单，准备关闭订单。order："+entity.getOrderSn());
       channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
    }

}
